package org.company.mars.activemq.spring;

import java.util.Date;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.company.earth.bean.Person;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

@Service
public class JmsSender {
    
    private JmsTemplate jmsTemplate;
    
    private Destination destination;
    
    public void simpleSend() {
        this.jmsTemplate.send(destination, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                Message message = session.createMessage();
                message.setBooleanProperty("flag", true);
                message.setStringProperty("message", "hello spring,I'm activeMQ test case!!");
                return message;
            }
        });
    }
    
    public void convertSend() {
        Person person = new Person();
        person.setId(1);
        person.setFirstName("hu");
        person.setLastName("changwei");
        person.setBirthday(new Date());
        this.jmsTemplate.convertAndSend(this.destination, person);
    }

    /**
     * @return the jmsTemplate
     */
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }

    /**
     * @param jmsTemplate the jmsTemplate to set
     */
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    /**
     * @return the destination
     */
    public Destination getDestination() {
        return destination;
    }

    /**
     * @param destination the destination to set
     */
    public void setDestination(Destination destination) {
        this.destination = destination;
    }
}
